package com.zhx.experiment.controller;

import com.zhx.experiment.entity.User;
import com.zhx.experiment.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.LinkedList;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/")
    public String gologin(){
        return "login.html";
    }

    @PostMapping(value = "login")
    public String login(Model model,User user) {
        User loginUser = userService.loginUser(user);
        if (loginUser != null) {
            model.addAttribute("username",loginUser.getUsername());
            return "index.html";
        }else{
            model.addAttribute("msg","用户名或密码输入错误");
            return "login.html";
        }
    }
    @RequestMapping(value = "toRegister")
    public String toRegister(){
        return "register.html";
    }

    @RequestMapping(value = "toLogin")
    public String toLogin(){return "login.html";}

    @RequestMapping(value = "toIndex")
    public String toIndex(Model model,String username){
        model.addAttribute("username",username);
        return "index.html";
    }

    @RequestMapping(value = "toPs_ct")
    public String toPs_ct(Model model,String username){
        model.addAttribute("username",username);
        return "personal_center.html";
    }

    @RequestMapping(value = "toPs_pd")
    public String toPs_pd(Model model,String username){
        model.addAttribute("username",username);
    return "personal_password.html";}

    @RequestMapping(value = "toUs_mg")
    public String toUs_mg(ModelMap model,String username){
        model.addAttribute("username",username);

        List<User> users= userService.findAllUser();
        model.addAttribute("users",users);
        return "user_management.html";
    }

    @RequestMapping(value = "registerUser")
    public String registerUser(User user){
        userService.addUser(user);
        return "login.html";
    }

    @RequestMapping(value = "searchUser")
    public String searchUser(Model model,String username,String sc_username){
        if("".equals(sc_username)){
            List<User> users= userService.findAllUser();
            model.addAttribute("username",username);
            model.addAttribute("users",users);
            return "user_management.html";
        }
        User user=userService.queryUserByName(sc_username);
        List<User> users= new LinkedList<>();
        if(user!=null)
            users.add(user);
        model.addAttribute("username",username);
        model.addAttribute("users",users);
        return "user_management.html";
    }

    @RequestMapping(value = "updateUser")
    public String updateUser(Model model,String username,User user){
        model.addAttribute("username",user.getUsername());

        User newUser=userService.queryUserByName(user.getUsername());
        newUser.setEmail(user.getEmail());
        newUser.setAmount(user.getAmount());
        newUser.setBirth(user.getBirth());
        userService.updateUser(newUser);
        return "index";
    }

    @RequestMapping(value = "updateUserPsd")
    public String updateUserPsd(Model model,User user){
        User newUser=userService.queryUserByName(user.getUsername());
        newUser.setPassword(user.getPassword());
        userService.updateUser(newUser);
        return "login.html";
    }

    @RequestMapping(value = "deleteUser")
    public String deleteUser(Model model,String username,String dl_username){
        model.addAttribute("username",username);
        userService.deleteUserByName(dl_username);

        List<User> users= userService.findAllUser();
        model.addAttribute("users",users);
        return "user_management.html";
    }
}
